package com.lcredu.lcrsnack.commodity.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author kirito
 * @create 2023-11-05 9:18
 */
public class FileUtil {
    public static final String UPLOAD_FILE = "D:\\idea\\lcr_lcrliving_project\\myproject\\lcrsnack\\lcrsnack-commodity\\src\\main\\java\\com\\lcredu\\lcrsnack\\commodity\\images";

    public static String upload(MultipartFile file) throws IOException {
        final String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileName = UUID.randomUUID().toString().replace("-","")+"."+fileSuffix;
        String filename2 = file.getOriginalFilename();
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");

        String strDate = dateFormat.format(date);

        String filename = strDate + filename2 + "." + fileSuffix;
        File descFile = new File(UPLOAD_FILE, fileName);
        file.transferTo(descFile);

        String url = "/images/"+fileName;
        System.out.println(url);
        return url;
    }
}
